package com.fancy.music.controller.admin;

import com.fancy.common.dto.PageResult;
import com.fancy.common.dto.response.Result;
import com.fancy.music.entity.Artist;
import com.fancy.music.service.ArtistService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理员-歌手管理")
@RestController
@RequestMapping("/api/v1/admin/artists")
@RequiredArgsConstructor
public class AdminArtistController {

    private final ArtistService artistService;

    @ApiOperation(value = "获取歌手管理列表", notes = "管理员查看所有歌手信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "keyword", value = "搜索关键词", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "genre", value = "音乐风格", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "country", value = "国家/地区", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "status", value = "状态(0-禁用,1-正常)", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "int", example = "1"),
        @ApiImplicitParam(name = "size", value = "每页大小", paramType = "query", dataType = "int", example = "20")
    })
    @GetMapping
    public Result<PageResult<Artist>> getArtistManageList(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String genre,
            @RequestParam(required = false) String country,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        PageResult<Artist> result = artistService.searchArtists(keyword, genre, country, null, page, size);
        return Result.success(result);
    }

    @ApiOperation(value = "创建歌手", notes = "管理员添加新歌手")
    @ApiParam(name = "artist", value = "歌手信息", required = true)
    @PostMapping
    public Result<Long> createArtist(@RequestBody Artist artist) {
        Long artistId = artistService.createArtist(artist);
        if (artistId != null) {
            return Result.success(artistId);
        } else {
            return Result.fail("创建歌手失败");
        }
    }

    @ApiOperation(value = "更新歌手信息", notes = "管理员修改歌手信息")
    @ApiImplicitParam(name = "artistId", value = "歌手ID", required = true, paramType = "path", dataType = "long")
    @ApiParam(name = "artist", value = "歌手信息", required = true)
    @PutMapping("/{artistId}")
    public Result<Void> updateArtist(@PathVariable Long artistId, @RequestBody Artist artist) {
        artist.setArtistId(artistId);
        boolean success = artistService.updateArtist(artist);
        if (success) {
            return Result.success(null);
        } else {
            return Result.fail("更新歌手失败");
        }
    }

    @ApiOperation(value = "删除歌手", notes = "管理员删除指定歌手")
    @ApiImplicitParam(name = "artistId", value = "歌手ID", required = true, paramType = "path", dataType = "long")
    @DeleteMapping("/{artistId}")
    public Result<Void> deleteArtist(@PathVariable Long artistId) {
        boolean success = artistService.deleteArtist(artistId);
        if (success) {
            return Result.success(null);
        } else {
            return Result.fail("删除歌手失败");
        }
    }

    @ApiOperation(value = "批量删除歌手", notes = "管理员批量删除多个歌手")
    @ApiParam(name = "artistIds", value = "歌手ID列表", required = true)
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteArtists(@RequestBody List<Long> artistIds) {
        int successCount = 0;
        for (Long artistId : artistIds) {
            if (artistService.deleteArtist(artistId)) {
                successCount++;
            }
        }
        if (successCount > 0) {
            return Result.success(null);
        } else {
            return Result.fail("批量删除失败");
        }
    }

    @ApiOperation(value = "启用/禁用歌手", notes = "管理员修改歌手状态")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "artistId", value = "歌手ID", required = true, paramType = "path", dataType = "long"),
        @ApiImplicitParam(name = "status", value = "状态(0-禁用,1-启用)", required = true, paramType = "query", dataType = "int")
    })
    @PutMapping("/{artistId}/status")
    public Result<Void> toggleArtistStatus(@PathVariable Long artistId, @RequestParam Integer status) {
        boolean success = artistService.toggleArtistStatus(artistId, status);
        if (success) {
            return Result.success(null);
        } else {
            return Result.fail("修改歌手状态失败");
        }
    }
}
